package com.ns.dcjh.receiver;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.ns.dcjh.bean.MessageEvent;
import com.ns.dcjh.constant.Constant;
import com.ns.dcjh.ui.activity.SplashActivity;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

public class MyJPushMessageReceiver extends JPushMessageReceiver {

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
//        super.onMessage(context, customMessage);
        //消息到达 可指定类型，指定操作
        Log.e("JPush", "onMessage: " + customMessage.message + "/" + customMessage.extra);

        if (!customMessage.extra.contains("dcjh")) {
            return;
        }

        try {
            MessageEvent msgEvent = new MessageEvent(Constant.MSG_EVENT_RECEIVED_MSG);
            JSONObject extras = new JSONObject();
            extras.put("action", "message");
            extras.put("status", true);
            extras.put("message", "");
            JSONObject result = new JSONObject();
            result.put("title", customMessage.title);
            result.put("msg_content", customMessage.message);
            result.put("content_type", customMessage.contentType);
            result.put("extras", JSONObject.parseObject(customMessage.extra));
            extras.put("result", result);
            msgEvent.setExtras(extras);
            EventBus.getDefault().post(msgEvent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
//        super.onNotifyMessageArrived(context, notificationMessage);
        // 通知到达
        Log.e("JPush", "onNotifyMessageArrived: " + notificationMessage.notificationExtras);

        if (!notificationMessage.notificationExtras.contains("dcjh")) {
            return;
        }

        try {
            MessageEvent msgEvent = new MessageEvent(Constant.MSG_EVENT_RECEIVED_MSG);
            JSONObject extras = new JSONObject();
            extras.put("action", "notification_clicked");
            extras.put("status", true);
            extras.put("message", notificationMessage.notificationContent);
            JSONObject result = new JSONObject();
            result.put("extras", JSONObject.parseObject(notificationMessage.notificationExtras));
            extras.put("result", result);
            msgEvent.setExtras(extras);
            EventBus.getDefault().post(msgEvent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
//        super.onNotifyMessageOpened(context, notificationMessage);
        // 点击通知栏消息 可指定类型，指定操作
        Log.e("JPush", "onNotifyMessageOpened: " + notificationMessage.notificationExtras);

        if (!notificationMessage.notificationExtras.contains("dcjh")) {
            return;
        }

        Intent intentNew = new Intent(context, SplashActivity.class); //自定义打开的界面
        intentNew.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intentNew);

        try {
            MessageEvent msgEvent = new MessageEvent(Constant.MSG_EVENT_RECEIVED_MSG);
            JSONObject extras = new JSONObject();
            extras.put("action", "notification_clicked");
            extras.put("status", true);
            extras.put("message", notificationMessage.notificationContent);
            JSONObject result = new JSONObject();
            result.put("extras", JSONObject.parseObject(notificationMessage.notificationExtras));
            extras.put("result", result);
            msgEvent.setExtras(extras);
            EventBus.getDefault().post(msgEvent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRegister(Context context, String s) {
        super.onRegister(context, s);
        // 保存极光id
        Log.e("JPush", "onRegister: id=" + s);

    }

    @Override
    public void onConnected(Context context, boolean b) {
        super.onConnected(context, b);
        Log.e("JPush", "onConnected: " + b);
        if (b) {
            // 链接监听 保存极光id
            String registrationID = JPushInterface.getRegistrationID(context.getApplicationContext());
            Log.e("JPush", "onConnected: 连接成功获取id=" + registrationID);
        }
    }
}
